package com.jjkj;


import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;
import com.jfinal.plugin.activerecord.generator.TableMeta;
import io.jboot.codegen.CodeGenHelpler;
import io.jboot.codegen.model.JbootBaseModelGenerator;
import io.jboot.codegen.model.JbootModelGenerator;
import io.jboot.codegen.service.JbootServiceImplGenerator;
import io.jboot.codegen.service.JbootServiceInterfaceGenerator;
import io.jboot.utils.StrUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by hello on 2018/1/8.
 */
public class _ModelGenerator extends JbootModelGenerator {

    public _ModelGenerator(String modelPackageName, String baseModelPackageName, String modelOutputDir) {
        super(modelPackageName, baseModelPackageName, modelOutputDir);
    }


    public static void run(String basePackage, String prefix) {
        doGenerate(basePackage, prefix);
    }

    /**
     * 只生成某些表
     */
    public static void runWith(String modelPackage, String includeTables) {
        doGenerateWith(modelPackage, includeTables);
    }

    /**
     * 只生成某些表,排除无主键表
     */
    public static void runWith(String modelPackage, String includeTables, String excludeTables) {
        doGenerateWith(modelPackage, includeTables, excludeTables, null);
    }

    /**
     * 只生成某些表,排除无主键表
     */
    public static void runWith(String modelPackage, String includeTables, String excludeTables, String prefix) {
        doGenerateWith(modelPackage, includeTables, excludeTables, prefix);
    }

    public static void doGenerate(String basePackage, String prefix) {
        doGenerateWith(basePackage, null, null, prefix);
    }


    /**
     * 只生某些表
     */
    public static void doGenerateWith(String basePackage, String includeTables) {
        doGenerateWith(basePackage, includeTables, null, null);
    }

    /**
     * 只生某些表
     */
    public static void doGenerateWith(String basePackage, String includeTables, String excludeTables, String prefix) {

        String modelPackage = basePackage + ".model";
        String baseModelPackage = modelPackage + ".base";

        String modelDir = PathKit.getWebRootPath() + "/src/main/java/" + modelPackage.replace(".", "/");
        String baseModelDir = PathKit.getWebRootPath() + "/src/main/java/" + baseModelPackage.replace(".", "/");

        System.out.println("start generate...");
        System.out.println("generate dir:" + modelDir);
        MetaBuilder metaBuilder = CodeGenHelpler.createMetaBuilder();
        if (StrUtil.notBlank(prefix)) {
            metaBuilder.setRemovedTableNamePrefixes(StrUtil.splitToSet(prefix, ",").toArray(new String[]{}));
        }
        if (StrUtil.notBlank(excludeTables)) {
            metaBuilder.addExcludedTable(StrUtil.splitToSet(excludeTables, ",").toArray(new String[]{}));
        }
        List<TableMeta> tableMetaList = metaBuilder.build();
        withIncludeTables(tableMetaList, includeTables);

        new JbootBaseModelGenerator(baseModelPackage, baseModelDir).generate(tableMetaList);
        new io.jboot.codegen.model.JbootModelGenerator(modelPackage, baseModelPackage, modelDir).generate(tableMetaList);

        System.out.println("model generate finished !!!");

        String servicePackage = basePackage + ".service"; // service 层的接口包名
        String serviceImplPackage = servicePackage; // service 层的接口实现类包名

        //开始生成代码
        new JbootServiceInterfaceGenerator(servicePackage, modelPackage).generate();
        new JbootServiceImplGenerator(serviceImplPackage, modelPackage).generate();

    }

    /**
     * 指定表
     *
     * @param list
     * @param includeTables
     */
    public static void withIncludeTables(List<TableMeta> list, String includeTables) {
        if (StringUtils.isNotBlank(includeTables)) {
            List<TableMeta> newTableMetaList = new ArrayList<>();
            Set<String> includeTableSet = StrUtil.splitToSet(includeTables.toLowerCase(), ",");

            for (TableMeta tableMeta : list) {
                if (includeTableSet.contains(tableMeta.name.toLowerCase())) {
                    newTableMetaList.add(tableMeta);
                }
            }
            list.clear();
            list.addAll(newTableMetaList);
        }
    }

    public static void main(String[] args) {
        doGenerate("com.jjkj", "");
    }

}
